package com.example.lib_resouce

import android.content.Context
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.annotation.Interceptor
import com.alibaba.android.arouter.facade.callback.InterceptorCallback
import com.alibaba.android.arouter.facade.template.IInterceptor
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.tencent.mmkv.MMKV
import com.yuwei.common.path.RouterPath

/**
 *Token路由拦截器
 */

@Interceptor(priority = 4, name = "路由Token拦截器")
class RouterTokenInterceptor :IInterceptor{
    private lateinit var mContext:Context

    override fun init(context: Context?) {
        if (context!=null){
            mContext=context
        }
    }

    override fun process(postcard: Postcard, callback: InterceptorCallback) {
        //如果已经是登录页面 就直接返回
        val loginToken = SPUtils.getInstance().getString("token")
        if (loginToken.isNullOrEmpty() && !postcard.path.equals(RouterPath.MODULE_LOGIN_ACTIVITY)){
            ARouter.getInstance().build(RouterPath.MODULE_LOGIN_ACTIVITY).navigation()
        }else{
            callback.onContinue(postcard)
        }
    }
}